var NWCorner = argument0;
var globalTileList = argument1;
var roomGraph = argument2;
var pathGraph = argument3;
var graphHeight = array_height_2d(pathGraph);
var graphWidth = array_length_2d(pathGraph, 0);

var localTileArray = 0;
var entranceTile = 0;
var exitTile = 0;

for (var i = 0; i < ds_list_size(globalTileList); i++)
{
    var tile = ds_list_find_value(globalTileList, i);
    //global processing
    if ((tile[1] + 1 < graphWidth && !roomGraph[tile[0], tile[1] + 1] && isPathTile(pathGraph, tile[0], tile[1] + 1)) ||
        (tile[0] - 1 > -1 && !roomGraph[tile[0] - 1, tile[1]] && isPathTile(pathGraph, tile[0] - 1, tile[1])) ||
        (tile[1] - 1 > -1 && !roomGraph[tile[0], tile[1] - 1] && isPathTile(pathGraph, tile[0], tile[1] - 1)) ||
        (tile[0] + 1 < graphHeight && !roomGraph[tile[0] + 1, tile[1]] && isPathTile(pathGraph, tile[0] + 1, tile[1])))
    {
        tile[0] -= NWCorner[0];
        tile[1] -= NWCorner[1];
        if(roomGraph[tile[0] + NWCorner[0], tile[1] + NWCorner[1]] == 1)
        {
            exitTile = tile;
        }
        else if (roomGraph[tile[0] + NWCorner[0], tile[1] + NWCorner[1]] == 2)
        {
            entranceTile = tile; 
        }        
        else if (!is_array(entranceTile) && !is_array(exitTile))
        {
            entranceTile = tile;
        }
        else if(pathGraph[entranceTile[0] + NWCorner[0], entranceTile[1] + NWCorner[1]] > 
                pathGraph[tile[0] + NWCorner[0], tile[1] + NWCorner[1]])
        {
            exitTile = entranceTile
            entranceTile = tile;
        }
        else
        {
            exitTile = tile;
        }
        tile[0] += NWCorner[0];
        tile[1] += NWCorner[1];  
    }
    
    
    
    //local processing
    tile[0] -= NWCorner[0];
    tile[1] -= NWCorner[1];
    localTileArray[tile[0], tile[1]] = 1;
    
    // make sure the array does not get jagged
    var maxWidth = 0;
    var maxHeight = array_height_2d(localTileArray);
    //find the max width
    
    for (var j = 0; j < maxHeight; j++)
    {
        var thisWidth = array_length_2d(localTileArray, j);
        if (thisWidth > maxWidth)
        {
            maxWidth = thisWidth;
        } 
    }
    //pad
    for (var j = 0; j < maxHeight; j++)
    {
        var thisWidth = array_length_2d(localTileArray, j);
        if (thisWidth < maxWidth)
        {
            for (var k = thisWidth; k < maxWidth; k++)
            {
                localTileArray[j, k] = 0;
            }
        }
    }
    //Better algorithm?    
}

var floorArea = areaFactory(localTileArray);
floorArea.tileX = NWCorner[1];
floorArea.tileY = NWCorner[0];
floorArea.entranceTile = entranceTile;
floorArea.exitTile = exitTile;
if (is_array(entranceTile))
{
    floorArea.tiles[entranceTile[0], entranceTile[1]].sprite_index = spr_Entrance;
}
if (is_array(exitTile))
{
    floorArea.tiles[exitTile[0], exitTile[1]].sprite_index = spr_Exit;
}

return floorArea;
